package com.dks.controller;


import com.aliyuncs.exceptions.ClientException;
import com.dks.dto.UserDto;
import com.dks.exception.BaseException;
import com.dks.repository.IUsersRepository;
import com.dks.result.Result;
import com.dks.result.ResultInfo;
import com.dks.result.ResultModel;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
@RequestMapping("users")
public class UsersController {

    private IUsersRepository usersRepository;

    @Autowired
    public UsersController(final IUsersRepository usersRepositoryIn) {
        this.usersRepository = usersRepositoryIn;
    }


    @GetMapping("{id}")
    public ResultModel<Object> select(@PathVariable("id") Integer id) {
        return usersRepository.select(id);
    }

    /**
     * @Author: Ah Nan
     * @Description:获取用户手机号验证码
     * @Param: tel
     * @return: ResultModel
     * @Date: 2020/4/19
     */
    @GetMapping("get-tel-code")
    public ResultModel<Object> getTelCode(String tel) throws BaseException, ClientException {

        if (tel == null || tel.isEmpty()) {
            return Result.makeResult(ResultInfo.ERR_PARAM_NULL, tel);
        }
        return usersRepository.getTelCode(tel);
    }


    /**
     * @Author: Ah Nan
     * @Description: 家长手机号验证码登录
     * @Date: 10:42 2020/4/20
     * @Modificd By:
     * @Param: userType 用户类型 0 家长、1 老师
     * @return:
     * @throw: 请描述异常信息
     */
    @GetMapping("app-login")
    public ResultModel<Object> appLogin(String tel, String code, String wechatCode) {

        if (tel == null || tel.isEmpty()) {
            return Result.makeResult(ResultInfo.ERR_PARAM_NULL, "tel");
        }
        if (code == null || code.isEmpty()) {
            return Result.makeResult(ResultInfo.ERR_PARAM_NULL, "code");
        }
        if (wechatCode == null || wechatCode.isEmpty()) {
            return Result.makeResult(ResultInfo.ERR_PARAM_NULL, "wechatCode");
        }
        return usersRepository.appLogin(tel, code, wechatCode);
    }


    /**
     * @Author: Ah Nan
     * @Description: 老师手机号验证码登录
     * @Date: 10:42 2020/4/20
     * @Modificd By:
     * @Param: userType 用户类型 0 家长、1 老师
     * @return:
     * @throw: 请描述异常信息
     */
    @GetMapping("tc-app-login")
    public ResultModel<Object> tcAppLogin(String tel, String code, String wechatCode) {

        if (tel == null || tel.isEmpty()) {
            return Result.makeResult(ResultInfo.ERR_PARAM_NULL, "tel");
        }
        if (code == null || code.isEmpty()) {
            return Result.makeResult(ResultInfo.ERR_PARAM_NULL, "code");
        }
        if (wechatCode == null || wechatCode.isEmpty()) {
            return Result.makeResult(ResultInfo.ERR_PARAM_NULL, "wechatCode");
        }
        return usersRepository.tcAppLogin(tel, code, wechatCode);
    }


    /**
     * @Author: Ah Nan
     * @Description: 微信登录
     * @Date: 14:05 2020/4/21
     * @Modificd By:
     * @Param: wechatCode 微信开放平台code
     * @return:
     * @throw: 请描述异常信息
     */
    @GetMapping("wechat-login")
    public ResultModel<Object> wechatLogin( String wechatCode)throws Exception {
        if (wechatCode == null || wechatCode.isEmpty()) {
            return Result.makeResult(ResultInfo.ERR_PARAM_NULL, "code");
        }
        return usersRepository.wechatLogin(wechatCode);
    }

    /**
     * @Author: Ah Nan
     * @Description: 微信登录绑定用户信息
     * @Date: 14:05 2020/4/21
     * @Modificd By:
     * @Param: wechatCode 微信开放平台code
     * @return:
     * @throw: 请描述异常信息
     */
    @PostMapping("binding-wechat-user")
    public ResultModel<Object> bindingWechatUser(@RequestBody UserDto userDto)throws Exception {
        if (userDto.getOpenid() == null || userDto.getOpenid().isEmpty()) {
            return Result.makeResult(ResultInfo.ERR_PARAM_NULL, "openid");
        }
        if (userDto.getNickname() == null || userDto.getNickname().isEmpty()) {
            return Result.makeResult(ResultInfo.ERR_PARAM_NULL, "Nickname");
        }
        if (userDto.getProfilePhotoUrl() == null || userDto.getProfilePhotoUrl().isEmpty()) {
            return Result.makeResult(ResultInfo.ERR_PARAM_NULL, "ProfilePhotoUrl");
        }
        if (userDto.getSex() == null ) {
            return Result.makeResult(ResultInfo.ERR_PARAM_NULL, "sex");
        }
        if (userDto.getProvince() == null || userDto.getProvince().isEmpty()) {
            return Result.makeResult(ResultInfo.ERR_PARAM_NULL, "Province");
        }
        if (userDto.getCity() == null || userDto.getCity().isEmpty()) {
            return Result.makeResult(ResultInfo.ERR_PARAM_NULL, "City");
        }
        if (userDto.getDistrict() == null || userDto.getDistrict().isEmpty()) {
            return Result.makeResult(ResultInfo.ERR_PARAM_NULL, "District");
        }
        if (userDto.getLoginName() == null || userDto.getLoginName().isEmpty()) {
            return Result.makeResult(ResultInfo.ERR_PARAM_NULL, "loginName");
        }
        return usersRepository.bindingWechatUser(userDto);
    }



    /**
     * @Author: Ah Nan
     * @Description: 获取手机号是否绑定过微信
     * @Date: 14:05 2020/4/21
     * @Modificd By:
     * @Param: tel 手机号
     * @return: data 0：代表未绑定，1：代表已绑定
     * @throw: 请描述异常信息
     */
    @GetMapping(value = "get-wechat-bind-byuser")
    public  ResultModel<Object> getWeChatBindByUser(final String tel) {

        if (tel == null || tel.isEmpty()) {
            return Result.makeResult(ResultInfo.ERR_PARAM_NULL, "tel");
        }

        return usersRepository.getWeChatBindByUser(tel);
    }

    /**
     * @Author: Ah Nan
     * @Description: 微信注册并登录
     * @Date: 14:05 2020/4/21
     * @Modificd By:
     * @Param: tel 用户手机号
     * @Param: randomNumParam 验证码
     * @Param: openid 微信开放平台id
     * @return:
     * @throw: 请描述异常信息
     */
    @GetMapping("wechat-register-login")
    public ResultModel<Object> wechatRegisterLogin( String tel,final String randomNumParam, String openid, Integer userType)throws Exception {
        if (tel == null || tel.isEmpty()) {
            return Result.makeResult(ResultInfo.ERR_PARAM_NULL, "tel");
        }
        if (randomNumParam == randomNumParam || tel.isEmpty()) {
            return Result.makeResult(ResultInfo.ERR_PARAM_NULL, "randomNumParam");
        }
        if (openid == null || openid.isEmpty()) {
            return Result.makeResult(ResultInfo.ERR_PARAM_NULL, "openid");
        }
        if (null == userType) {
            return Result.makeResult(ResultInfo.ERR_PARAM_NULL, "userType");
        }
        return usersRepository.wechatRegisterLogin(tel,randomNumParam,openid,userType);
    }


    /**
     * @Author: Ah Nan
     * @Description: 获取用户信息
     * @Date: 14:05 2020/4/21
     * @Modificd By:
     * @Param: loginName 手机号
     * @return: data 0：代表未绑定，1：代表已绑定
     * @throw: 请描述异常信息
     */
    @GetMapping(value = "get-user-info")
    public  ResultModel<Object> getUserInfo( Integer userId) {

        if (userId == null ) {
            return Result.makeResult(ResultInfo.ERR_PARAM_NULL, "userId");
        }

        return usersRepository.getUserInfo(userId);
    }


}
